package team.wuyou.util.proxyWeb;
import java.io.IOException;
import java.util.Scanner;

public class ProxyHost {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("网络代理服务");
        System.out.println("1.使用默认");
        System.out.println("2.自定义配置");
        System.out.println("3.清除代理");
        System.out.println("0.退出");
        String next = sc.next();
        Boolean start=true;
        //while (start) {
            if (next.equals("1")) {
                // 设置代理服务器地址和端口 香港精品
                String proxyHost = "c9dab5f5-s66hs0-scbq9c-1q0ue.hkt.gotochinatown.net";
                String proxyPort = "80";
                SystemProxyExample(proxyHost,proxyPort);
            }
            if (next.equals("2")) {
                //手动设置代理
                System.out.println("请输入代理服务器地址：");
                String proxyHost = sc.next();
                System.out.println("请输入代理服务器端口：");
                String proxyPort = sc.next();
                SystemProxyExample(proxyHost,proxyPort);
            }
            if (next.equals("3")) {
                //清除代理
                DisableProxyExample();
            }
            if (next.equals("0")) {
                start=false;
            }

        //}


    }

    //netsh winhttp set proxy proxy-server="c9dab5f5-s66hs0-scbq9c-1q0ue.hkt.gotochinatown.net:80"
    public static void SystemProxyExample (String proxyHost,String proxyPort) {
        // 构建netsh命令
        String command = "netsh winhttp set proxy proxy-server=\"" + proxyHost + ":" + proxyPort + "\"";

        try {
            // 执行netsh命令
            ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", command);
            Process process = processBuilder.start();

            // 等待命令执行完成
            int exitCode = process.waitFor();
            // 打印命令执行结果
            System.out.println("Command executed with exit code: " + exitCode);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void DisableProxyExample  () {
        // 构建netsh命令来禁用代理
        String command = "netsh winhttp reset proxy";

        try {
            // 执行netsh命令
            ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", command);
            Process process = processBuilder.start();

            // 等待命令执行完成
            int exitCode = process.waitFor();

            // 打印命令执行结果
            System.out.println("Command executed with exit code: " + exitCode);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        }
}
